/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rg1jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author apple
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String driver="com.mysql.jdbc.Driver";
        try {
            Class.forName(driver);
            System.out.println("JDBC Driver is loaded success!");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("JDBC Driver is loaded fail!");
        }
        String URL="jdbc:mysql://172.28.22.100:3306/rg1db";
        try {
            Connection conn=DriverManager.getConnection(URL,"user", "12345678");
            String sql="select * from student";
            Statement stmt=conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd=rs.getMetaData();
            int colCount=rsmd.getColumnCount();
            for(int i=1;i<=colCount;i++)
            {
                System.out.print(rsmd.getColumnName(i)+" ; ");
            }
            System.out.println("");
            while(rs.next())
            {
                System.out.print(rs.getInt("id")+" ");
                System.out.print(rs.getString("name")+" ");
                System.out.print(rs.getString("phone")+" ");
                System.out.print(rs.getString("address")+" ");
                System.out.print(rs.getString("birthday")+" ");
                System.out.println("");
            }
            
            sql="insert into student(name,phone,address,birthday) values('钢铁侠','8723648','米奇煲','"+DateUtil.getStringDate()+"')";
            System.out.println(sql);
            stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
